<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
    <title>Functional Java</title>
    <link href="../style.css" type="text/css" rel="stylesheet">
    <link href="../syntaxhighlighter-1.5.1/dp.SyntaxHighlighter/Styles/SyntaxHighlighter.css"
          rel="stylesheet" type="text/css">
    <meta http-equiv="Content-Language" content="en">
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <meta content="Tony Morris" name="author">
    <meta content="Copyright Tony Morris 2008-2010" name="copyright">
    <meta content="Functional,Java,Programming" name="keywords">
    <meta content="Functional Java" name="description">
    <script type="text/javascript">
        var home = new Image();
        home.src = "../images/buttons/home/1/over.png";
        var examples = new Image();
        examples.src = "../images/buttons/examples/1/over.png";
        var download = new Image();
        download.src = "../images/buttons/download/1/over.png";
        var api = new Image();
        api.src = "../images/buttons/api/1/over.png";
        var source = new Image();
        source.src = "../images/buttons/source/1/over.png";
        var community = new Image();
        community.src = "../images/buttons/community/1/over.png";
        var issues = new Image();
        issues.src = "../images/buttons/issues/1/over.png";
    </script>
    <script type="text/javascript">

        var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
        document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

    </script>
    <script type="text/javascript">

        var pageTracker = _gat._getTracker("UA-4485773-1");
        pageTracker._initData();
        pageTracker._trackPageview();

    </script>
</head>
<body>
<div class="logo">
    <img src="../images/logo-600x144.png" alt="Functional Java">
</div>
<hr>
<div class="menu">
    <p>
        <a href="../">
            <img src="../images/buttons/home/1/up.png" alt="Home" onmouseout="src='../images/buttons/home/1/up.png'"
                 onmouseover="src='../images/buttons/home/1/over.png'">
        </a>
    </p>

    <p>
        <a href="../examples/">
            <img src="../images/buttons/examples/1/up.png" alt="Examples"
                 onmouseout="src='../images/buttons/examples/1/up.png'"
                 onmouseover="src='../images/buttons/examples/1/over.png'">
        </a>
    </p>

    <p>
        <a href="../download/">
            <img src="../images/buttons/download/1/up.png" alt="Download"
                 onmouseout="src='../images/buttons/download/1/up.png'"
                 onmouseover="src='../images/buttons/download/1/over.png'">
        </a>
    </p>

    <p>
        <a href="http://functionaljava.googlecode.com/svn/artifacts/3.0/javadoc/index.html">
            <img src="../images/buttons/api/1/up.png" alt="API" onmouseout="src='../images/buttons/api/1/up.png'"
                 onmouseover="src='../images/buttons/api/1/over.png'">
        </a>
    </p>

    <p>
        <a href="../source/">
            <img src="../images/buttons/source/1/up.png" alt="Source" onmouseout="src='../images/buttons/source/1/up.png'"
                 onmouseover="src='../images/buttons/source/1/over.png'">
        </a>
    </p>

    <p>
        <a href="../community/">
            <img src="../images/buttons/community/1/up.png" alt="Community"
                 onmouseout="src='../images/buttons/community/1/up.png'"
                 onmouseover="src='../images/buttons/community/1/over.png'">
        </a>
    </p>

    <p>
        <a href="http://code.google.com/p/functionaljava/issues/list">
            <img src="../images/buttons/issues/1/up.png" alt="Issues" onmouseout="src='../images/buttons/issues/1/up.png'"
                 onmouseover="src='../images/buttons/issues/1/over.png'">
        </a>
    </p>
</div>
<div class="main">
    <div>
        <h4>Examples BGGA</h4>
        <h6><em><a href="../examples/1.5">Examples (1.5)</a></em></h6>

        <p>
            These examples use the Java 7 closures proposal and have
            <a href="../examples/1.5">corresponding examples that use traditional Java syntax</a> in the
            Functional Java <a href="../download">download</a>. The BGGA closure prototype is required to
            compile these examples, which can be downloaded from <a href="http://javac.info/">http://javac.info/</a>.
            Any JVM version 1.5 or higher is required to use the Functional Java library or run the compiled examples.
        </p>
        <a name="Examples.list"></a>

        <div>
            <ul>
                <li><a href="#Array.exists">Array exists</a></li>
                <li><a href="#Array.filter">Array filter</a></li>
                <li><a href="#Array.foldLeft">Array foldLeft</a></li>
                <li><a href="#Array.forall">Array forall</a></li>
                <li><a href="#Array.map">Array map</a></li>
                <li><a href="#List.map">List map</a></li>
                <li><a href="#Option.bind">Option bind</a></li>
                <li><a href="#Option.filter">Option filter</a></li>
                <li><a href="#Option.map">Option map</a></li>
            </ul>
            <hr>
            <a name="Array.exists"></a><h5>Array exists <a
                href="http://functionaljava.googlecode.com/svn/artifacts/3.0/demo/bgga/Array_exists.java">&#x00A7;</a>
        </h5>

            <p>Checks for the existence of a String that has all lower case characters. In this case it is true (since,
                the case of &quot;what&quot;), but without this specific case, it is false. This example uses
                List.forall to test the characters of the String.</p><pre class="java:nocontrols" name="code"
                                                                          id="Array.exists.example">import fj.data.Array;
import static fj.data.Array.array;
import static fj.data.List.fromString;
import static java.lang.Character.isLowerCase;

public final class Array_exists {
  public static void main(final String[] args) {
    final Array&lt;String&gt; a = array(&quot;Hello&quot;, &quot;There&quot;, &quot;what&quot;, &quot;DAY&quot;, &quot;iS&quot;, &quot;iT&quot;);
    final boolean b = a.exists({String s =&gt; fromString(s).forall({char c =&gt; isLowerCase(c)})});
    System.out.println(b); // true (&quot;what&quot; provides the only example; try removing it)
  }
}
</pre>
            <hr>
            <a name="Array.filter"></a><h5>Array filter <a
                href="http://functionaljava.googlecode.com/svn/artifacts/3.0/demo/bgga/Array_filter.java">&#x00A7;</a>
        </h5>

            <p>Removes elements from an array that do not meet a certain criteria. In this case, we are using an array
                of integers and the filter removes any odd numbers.</p><pre class="java:nocontrols" name="code"
                                                                            id="Array.filter.example">import fj.data.Array;
import static fj.data.Array.array;
import static fj.pre.Show.arrayShow;
import static fj.pre.Show.intShow;

public final class Array_filter {
  public static void main(final String[] args) {
    final Array&lt;Integer&gt; a = array(97, 44, 67, 3, 22, 90, 1, 77, 98, 1078, 6, 64, 6, 79, 42);
    final Array&lt;Integer&gt; b = a.filter({int i =&gt; i % 2 == 0});
    arrayShow(intShow).println(b); // {44,22,90,98,1078,6,64,6,42}
  }
}
</pre>
            <hr>
            <a name="Array.foldLeft"></a><h5>Array foldLeft <a
                href="http://functionaljava.googlecode.com/svn/artifacts/3.0/demo/bgga/Array_foldLeft.java">
            &#x00A7;</a></h5>

            <p>Reduces the list applying a function per element. In this case, the fold sums the elements with a
                starting value of 0. Since 0 + 97 + 44 + 67 + 3 + 22 + 90 + 1 + 77 + 98 + 1078 + 6 + 64 + 6 + 79 + 42 ==
                1774 the result of the fold is 1774.</p><pre class="java:nocontrols" name="code"
                                                             id="Array.foldLeft.example">import fj.data.Array;
import static fj.data.Array.array;

public final class Array_foldLeft {
  public static void main(final String[] args) {
    final Array&lt;Integer&gt; a = array(97, 44, 67, 3, 22, 90, 1, 77, 98, 1078, 6, 64, 6, 79, 42);
    final int b = a.&lt;Integer&gt;foldLeft({int i =&gt; {int j =&gt; i + j}}, 0);
    System.out.println(b); // 1774
  }
}
</pre>
            <hr>
            <a name="Array.forall"></a><h5>Array forall <a
                href="http://functionaljava.googlecode.com/svn/artifacts/3.0/demo/bgga/Array_forall.java">&#x00A7;</a>
        </h5>

            <p>Checks that all Strings in the array have lower case characters. In this case, the check fails because of
                the case of &quot;There&quot;, however, the removal of this case produces a result of true.</p><pre
                class="java:nocontrols" name="code" id="Array.forall.example">import fj.data.Array;
import static fj.data.Array.array;
import static fj.data.List.fromString;
import static java.lang.Character.isLowerCase;

public final class Array_forall {
  public static void main(final String[] args) {
    final Array&lt;String&gt; a = array(&quot;hello&quot;, &quot;There&quot;, &quot;what&quot;, &quot;day&quot;, &quot;is&quot;, &quot;it&quot;);
    final boolean b = a.forall({String s =&gt; fromString(s).forall({char c =&gt; isLowerCase(c)})});
    System.out.println(b); // false (&quot;There&quot; is a counter-example; try removing it)
  }
}
</pre>
            <hr>
            <a name="Array.map"></a><h5>Array map <a
                href="http://functionaljava.googlecode.com/svn/artifacts/3.0/demo/bgga/Array_map.java">&#x00A7;</a>
        </h5>

            <p>Maps a function across the array of integers. In this case, the function adds 42 to each element of the
                array to produce a new array.</p><pre class="java:nocontrols" name="code" id="Array.map.example">import fj.data.Array;
import static fj.data.Array.array;
import static fj.pre.Show.arrayShow;
import static fj.pre.Show.intShow;

public final class Array_map {
  public static void main(final String[] args) {
    final Array&lt;Integer&gt; a = array(1, 2, 3);
    final Array&lt;Integer&gt; b = a.map({int i =&gt; i + 42});
    arrayShow(intShow).println(b); // {43,44,45}
  }
}
</pre>
            <hr>
            <a name="List.map"></a><h5>List map <a
                href="http://functionaljava.googlecode.com/svn/artifacts/3.0/demo/bgga/List_map.java">&#x00A7;</a></h5>

            <p>Maps a function across a list of integers. This is similar to the Array map. This example adds 42 to each
                element of the list to produce a new list.</p><pre class="java:nocontrols" name="code"
                                                                   id="List.map.example">import static fj.data.List.list;
import fj.data.List;
import static fj.pre.Show.intShow;
import static fj.pre.Show.listShow;

public final class List_map {
  public static void main(final String[] args) {
    final List&lt;Integer&gt; a = list(1, 2, 3);
    final List&lt;Integer&gt; b = a.map({int i =&gt; i + 42});
    listShow(intShow).println(b); // [43,44,45]
  }
}
</pre>
            <hr>
            <a name="Option.bind"></a><h5>Option bind <a
                href="http://functionaljava.googlecode.com/svn/artifacts/3.0/demo/bgga/Option_bind.java">&#x00A7;</a>
        </h5>

            <p>Binds a function across the optional value type. The function checks if the contained value is even and
                if it is multiples that value by 3 and returns that new value. If the contained value is odd (or if
                there is no value), then no value is returned (none).</p><pre class="java:nocontrols" name="code"
                                                                              id="Option.bind.example">import fj.data.Option;
import static fj.data.Option.none;
import static fj.data.Option.some;
import static fj.pre.Show.intShow;
import static fj.pre.Show.optionShow;

public final class Option_bind {
  public static void main(final String[] args) {
    final Option&lt;Integer&gt; o1 = some(7);
    final Option&lt;Integer&gt; o2 = some(8);
    final Option&lt;Integer&gt; o3 = none();
    final Option&lt;Integer&gt; p1 = o1.bind({int i =&gt; i % 2 == 0 ? some(i * 3) : Option.&lt;Integer&gt;none()});
    final Option&lt;Integer&gt; p2 = o2.bind({int i =&gt; i % 2 == 0 ? some(i * 3) : Option.&lt;Integer&gt;none()});
    final Option&lt;Integer&gt; p3 = o3.bind({int i =&gt; i % 2 == 0 ? some(i * 3) : Option.&lt;Integer&gt;none()});
    optionShow(intShow).println(p1); // None
    optionShow(intShow).println(p2); // Some(24)
    optionShow(intShow).println(p3); // None
  }
}
</pre>
            <hr>
            <a name="Option.filter"></a><h5>Option filter <a
                href="http://functionaljava.googlecode.com/svn/artifacts/3.0/demo/bgga/Option_filter.java">&#x00A7;</a>
        </h5>

            <p>Removes the value from the optional value if it does not match a given predicate. In this case the
                condition for preservation is that the contained value is an even number.</p><pre
                class="java:nocontrols" name="code" id="Option.filter.example">import fj.data.Option;
import static fj.data.Option.none;
import static fj.data.Option.some;
import static fj.pre.Show.intShow;
import static fj.pre.Show.optionShow;

public final class Option_filter {
  public static void main(final String[] args) {
    final Option&lt;Integer&gt; o1 = some(7);
    final Option&lt;Integer&gt; o2 = none();
    final Option&lt;Integer&gt; o3 = some(8);
    final Option&lt;Integer&gt; p1 = o1.filter({int i =&gt; i % 2 == 0});
    final Option&lt;Integer&gt; p2 = o2.filter({int i =&gt; i % 2 == 0});
    final Option&lt;Integer&gt; p3 = o3.filter({int i =&gt; i % 2 == 0});
    optionShow(intShow).println(p1); // None
    optionShow(intShow).println(p2); // None
    optionShow(intShow).println(p3); // Some(8)
  }
}
</pre>
            <hr>
            <a name="Option.map"></a><h5>Option map <a
                href="http://functionaljava.googlecode.com/svn/artifacts/3.0/demo/bgga/Option_map.java">&#x00A7;</a>
        </h5>

            <p>Maps a function across the optional value type. The function adds 42 to any contained value.</p><pre
                class="java:nocontrols" name="code" id="Option.map.example">import fj.data.Option;
import static fj.data.Option.none;
import static fj.data.Option.some;
import static fj.pre.Show.intShow;
import static fj.pre.Show.optionShow;

public final class Option_map {
  public static void main(final String[] args) {
    final Option&lt;Integer&gt; o1 = some(7);
    final Option&lt;Integer&gt; o2 = none();
    final Option&lt;Integer&gt; p1 = o1.map({int i =&gt; i + 42});
    final Option&lt;Integer&gt; p2 = o2.map({int i =&gt; i + 42});
    optionShow(intShow).println(p1); // Some(49)
    optionShow(intShow).println(p2); // None
  }
}
</pre>
            <hr>
            <script type="text/javascript"
                    src="../syntaxhighlighter-1.5.1/dp.SyntaxHighlighter/Scripts/shCore.js"></script>
            <script type="text/javascript"
                    src="../syntaxhighlighter-1.5.1/dp.SyntaxHighlighter/Scripts/shBrushJava.js"></script>
            <script type="text/javascript">
                dp.SyntaxHighlighter.ClipboardSwf = 'flash/clipboard.swf';
                dp.SyntaxHighlighter.HighlightAll('code');
            </script>
        </div>
    </div>
</div>
<div class="copyright">
    <p>
        Copyright &copy; 2010
    </p>
</div>
</body>
</html>
